package com.learn.concurrent.aqs;

import java.util.concurrent.*;

/**
 * @author wangxing
 * @version 2020/9/4 14:10 Administrator
 */
public class LearnCyclicBarrier {
    public static final int NUM = 10;
    public static final CyclicBarrier cyclicBarrier = new CyclicBarrier(NUM);
    public static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(NUM, 20, 1, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10));

    public static void main(String[] args) {
        for(int i=0;i<NUM*2;i++){
            threadPoolExecutor.execute(()->{
                System.out.println("到达栅栏");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("走出栅栏");
            });
        }
        threadPoolExecutor.shutdown();
    }
}
